package com.sdonkey.score.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sdonkey.score.bean.UserLoginBean;
import com.sdonkey.score.bean.UserMsgBean;
import com.sdonkey.score.dao.IUserLoginDao;
import com.sdonkey.score.dao.IUserMsgDao;
import com.sdonkey.score.service.IUserMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ZhaoShihao
 * @version 1.0
 */
@Service
@Transactional
public class UserMsgServiceImpl implements IUserMsgService {
    @Autowired
    private IUserMsgDao msgDao;
    @Autowired
    private IUserLoginDao loginDao;

    @Override
    public void sendMsgForComplaint(Integer adId, Integer userId, String content) {
        UserMsgBean msg = new UserMsgBean();
        msg.setUser_id(userId);
        msg.setMsg_title("您的投诉已被受理");
        msg.setMsg_content("您投诉的广告（编号：" + adId + "）已被受理，投诉原因：<span style='font-color:red'>" + content + "</span>。感谢您对广告积分平台的支持！");
        msg.setMsg_prefix("系统通知");
        msg.setMsg_type("用户投诉");
        msgDao.insertMsg(msg);
    }

    @Override
    public int sendMsgForAddFriend(Integer userId, Integer frdId, String content) {
        try {
            if (msgDao.selectHasMsg("%<input type='hidden' id='userId' name='userId' value='" + userId + "'>%", frdId)) {
                return -2;
            }
            UserMsgBean msg = new UserMsgBean();
            UserLoginBean user = loginDao.getUser(userId);
            if (user != null) {
                msg.setUser_id(frdId);
                msg.setMsg_title("您有一条好友申请");
                StringBuilder sb = new StringBuilder();
                sb.append("用户<a href='javascript:void(0)' class='person' id='person'>").append(user.getName()).append("</a>请求添加您为好友,备注: ").append(content).append("</br>\n").append("是否同意并添加对方至你的好友列表？\n").append("        <div class='right'>\n").append("        <input type='hidden' id='userId' name='userId' value='").append(userId).append("'>\n").append("        <span class='agree inline-block btnBox handes' id='execute'>同意</span>\n").append("        <span class='refuse inline-block btnBox handes' id='unExecute'>拒绝</span>\n").append("        </div>");
                msg.setMsg_content(sb.toString());
                msg.setMsg_prefix("系统通知");
                msg.setMsg_type("添加好友");
                int i = msgDao.insertMsg(msg);
                if (i > 0) {
                    return i;
                }
            }
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int sendMsgForAddFriendRes(Integer userId, Integer frdId, boolean suc, Integer msgId) {
        try {
            UserMsgBean msg = new UserMsgBean();
            msg.setUser_id(userId);
            UserLoginBean frd = loginDao.getUser(frdId);
            //用户添加成功信息修改
            UserMsgBean msg1 = msgDao.selectUserMsgById(msgId);
            String content = msg1.getMsg_content();
            content = content.substring(0, content.length() - 173);
            StringBuilder sb = new StringBuilder(content);
            if (frd != null) {
                if (suc) {
                    msg.setMsg_title("好友添加成功");
                    msg.setMsg_content("用户" + frd.getName() + "已同意将您添加为好友。");
                    msg.setMsg_prefix("系统通知");
                    msg.setMsg_type("添加好友");
                    sb.append("        <span class='agree inline-block btnBox handes'>已同意</span>\n        </div>");
                } else {
                    msg.setMsg_title("好友添加失败");
                    msg.setMsg_content("用户" + frd.getName() + "拒绝将您添加为好友。");
                    msg.setMsg_prefix("系统通知");
                    msg.setMsg_type("添加好友");
                    sb.append("        <span class='agree inline-block btnBox handes'>已拒绝</span>\n        </div>");
                }
                int i = msgDao.insertMsg(msg);
                int j = msgDao.updateMsgForContent(msgId, sb.toString());
                if ((i + j) / 2 > 0) {
                    return 1;
                }
            }
            return -1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void sendMsgForAccountIncome(Integer userId, Double income, String incomeType) {
        UserMsgBean msg = new UserMsgBean();
        msg.setUser_id(userId);
        msg.setMsg_title("您有一条新的收入信息");
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        msg.setMsg_content("您于" + df.format(new Date()) + "通过" + incomeType + "获取收入" + income);
        msg.setMsg_prefix("系统通知");
        msg.setMsg_type("账户变动");
        msgDao.insertMsg(msg);
    }

    @Override
    public void sendMsgForAccountDefray(Integer userId, Double defray, String defrayType) {
        UserMsgBean msg = new UserMsgBean();
        msg.setUser_id(userId);
        msg.setMsg_title("您有一条新的支出信息");
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        msg.setMsg_content("您于" + df.format(new Date()) + "通过" + defrayType + "支出" + defray);
        msg.setMsg_prefix("系统通知");
        msg.setMsg_type("账户变动");
        msgDao.insertMsg(msg);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Page<UserMsgBean> getMsgByUserId(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return (Page<UserMsgBean>) msgDao.selectUserMsg(userId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public UserMsgBean getMsgById(Integer id) {
        int i = msgDao.updateMsgState(id, 1);
        return msgDao.selectUserMsgById(id);
    }

    @Override
    public int getMSgNumUnRead(Integer id) {
        return msgDao.selectUnReadNum(id);
    }
}
